package com.topdt.jbpm.web.struts.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.BaseAction;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.form.query.Option;
import com.topdt.jbpm.identity.Department;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.Role;
import com.topdt.jbpm.utils.TreeUtils;

@Namespace("/workflow")
@Action(value = "perConfig",
		results = {
		@Result(name = "perJson", type = "json", params = { "root", "perJson" })
		}
)
@Controller
@Scope("prototype")
/**
 *
* @author _zili
* @ClassName: PerConfigAction
* @Description:
* @date 2015年3月26日 上午11:15:28
 */
public class PerConfigAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private String selData;
	private String selType;
	private String perJson;

	public String asynLoadOrgs(){
		logger.debug("<进入>:asynLoadOrgs方法");
		String comId = AppContext.getCurrentComId();
		IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
		List<Department> deps = idService.getAllDepsAndUsers(comId);
		List<Option> ops = idService.getOptionsByDepartment(deps);
		//fengjinshou 20160121 将树中的多层级上的人员显示在第二级中-------------start
		String perTreeLevel = AppContext.getProperties("perTreeLevel");
		if(StringUtils.isNotBlank(perTreeLevel) && Constants.PER_TREE_LEVEL_TWOLEVEL.equals(perTreeLevel)){
			if(ops!=null && ops.size()>0){
				Option treeOpt =  ops.get(0);
				if(treeOpt.getChildren()!=null && treeOpt.getChildren().size()>0){
					for (Option option : treeOpt.getChildren()) {//公司下的部门或人
						if(option.getType().equals("dep")){
							//最终的三级
							List<Option> finalTreeLevel = new ArrayList<Option>();
							List<Option> threeLevel = option.getChildren();
							if(threeLevel!=null && threeLevel.size()>0){
								for (Option threeopt : threeLevel) {
									if(threeopt.getType().equals("per")){
										finalTreeLevel.add(threeopt);
									}else{
										addMoreLevel2ThreeLevel(finalTreeLevel, threeopt);
									}
								}
							}
							option.setChildren(finalTreeLevel);
						}
					}
				}
			}
		}
		//fengjinshou 20160121 人员显示层级-------------end
		Option root=ops.get(0);
		List<Map<String,String>> nodes = new ArrayList<Map<String,String>>();
		TreeUtils.getZtreeMapByOptionConfig(root,"0",nodes,selType,selData);
	    perJson = com.alibaba.fastjson.JSON.toJSONString(nodes);
	    return "perJson";
	}


	/**
	 * 将多级的人添加至三级
	 * @author 冯晋首
	 * @date   2016年1月21日 下午6:36:18
	 * @param finalTreeLevel
	 * @param opt
	 * @return
	 */
	public List<Option> addMoreLevel2ThreeLevel(List<Option> finalTreeLevel,Option opt){
		logger.debug("<进入>:addMoreLevel2ThreeLevel方法   将多级的人添加至三级");
		List<Option> optChildren = opt.getChildren();
		if(optChildren!=null && optChildren.size()>0){
			for (Option p : optChildren) {
				if(p.getType().equals("per")){
					finalTreeLevel.add(p);
				}else{
					addMoreLevel2ThreeLevel(finalTreeLevel, p);
				}
			}
		}
		return finalTreeLevel;
	}

	public String asynLoadDept(){
		logger.debug("<进入>:asynLoadDept方法");
		String comId = AppContext.getCurrentComId();
		IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
		List<Department> deps = idService.getDeps(comId);

		List<Option> ops = idService.getOptionsByDepartment(deps);
		Option root = ops.get(0);

		List<Map<String,String>> nodes = new ArrayList<Map<String,String>>();
		TreeUtils.getZtreeMapByOptionConfig(root,"0",nodes,selType,selData);
		perJson = com.alibaba.fastjson.JSON.toJSONString(nodes);
	    return "perJson";
	}

	public String asynLoadRole(){
		logger.debug("<进入>:asynLoadRole方法");
		String comId = AppContext.getCurrentComId();
		IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
		List<Role> roles = idService.getAllRoles(comId);
		perJson = com.alibaba.fastjson.JSON.toJSONString(roles);
		return "perJson";
	}

	public String getSelData() {
		return selData;
	}
	public void setSelData(String selData) {
		this.selData = selData;
	}
	public String getSelType() {
		return selType;
	}
	public void setSelType(String selType) {
		this.selType = selType;
	}
	public String getPerJson() {
		return perJson;
	}
	public void setPerJson(String perJson) {
		this.perJson = perJson;
	}

}
